//
//  FocusCollectionViewCell.m
//  CoolFM
//
//  Created by 曹曹 on 16/9/5.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "FocusCollectionViewCell.h"
#import "MineTableViewCell.h"
#import "CFMacro.h"
#import "GXNetTool.h"
#import "LoginViewController.h"
#import "FocusProgram.h"
#import "FocusProData.h"
#import "FocusProgramTableViewCell.h"

#define kFocusProgramTableViewCell @"FocusProgramTableViewCell"

@interface FocusCollectionViewCell () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) UILabel *radioLabel;
@property (nonatomic, strong) UILabel *programLabel;
@property (nonatomic, strong) UILabel *djLabel;
@property (nonatomic, strong) NSMutableArray *headerTitles;
@property (weak, nonatomic) IBOutlet UIView *unLoginView;

@end

@implementation FocusCollectionViewCell

- (void)setFocusProgram:(FocusProgram *)focusProgram {
    _focusProgram = focusProgram;
    [self.tableView reloadData];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if ([user objectForKey:@"login"]) {
        self.unLoginView.alpha = 0;
        [self setUpTableViewWhenLogined];
    } else {
        NSLog(@"此时没有登录");
    }
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(refreshUserFocusWhenLogined)
                                                 name:@"login"
                                               object:@"login"];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(refeshWhenUnlogined)
                                                 name:@"signOff"
                                               object:@"signOff"];
}
- (void)refreshUserFocusWhenLogined {
    self.tableView.alpha = 1;
    self.unLoginView.alpha = 0;
    [self.tableView reloadData];
}
- (void)refeshWhenUnlogined {
    self.tableView.alpha = 0;
    self.unLoginView.alpha = 1;
}
#pragma mark - 创建tableView
- (void)setUpTableViewWhenLogined {
    self.headerTitles = [NSMutableArray arrayWithObjects:@"电台", @"节目", @"主播", nil];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    kTalbleViewRegisterNib(@"FocusProgramTableViewCell", kFocusProgramTableViewCell);
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
// http://cdn.kufm.cn/playback2/M05/0D/E6/CgGIFVc2qKqANiUBAA-3LjQ14DM652.aac

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FocusProgramTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFocusProgramTableViewCell];
    if (_focusProgram) {
        cell.focusData = _focusProgram.data[indexPath.row];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *firstHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 20)];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"btn_list"] forState:UIControlStateNormal];
    button.frame = CGRectMake(SCREEN_WIDTH - 25, 0, 20, 20);
    [firstHeader addSubview:label];
    [firstHeader addSubview:button];
    label.text = _headerTitles[section];
    label.font = [UIFont systemFontOfSize:14];
    return firstHeader;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 25;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (IBAction)goToLoginVC:(id)sender {
    id responder = self.nextResponder;
    while (![responder isKindOfClass:[UIViewController class]] && responder != nil) {
        responder = [responder nextResponder];
    }
    UIViewController *vc = (UIViewController *)responder;
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [vc.navigationController pushViewController:loginVC animated:NO];
}

@end
